<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class peminjaman_bak extends CI_Controller {

    public function __construct() {
        parent::__construct();

        $this->load->library('auth');
        $this->load->library('base');
        $this->load->library('ajaxresponse');
        $this->load->library('datatables');
        $this->load->model("crud/m_mohon_kredit");
        $this->load->model("crud/m_pinjaman");
        $this->load->model("crud/m_angsuran");
        $this->load->model("crud/m_info_usaha");
        $this->load->model("crud/m_jenis_kredit");
    }

    function index() {
        $this->auth->is_logged_in('form/login', 2);
        $this->base->head_html('Form Peminjaman Kredit');
        $data["Usaha"] = $this->m_info_usaha->get("KODE_USAHA > 1");
        $data["Kredit"] = $this->m_jenis_kredit->get();
        if ($this->session->userdata("ID_MEMBER_TYPE") == 2)
            $this->load->view('pengurus/br-03/peminjaman', $data);
        else if ($this->session->userdata("ID_MEMBER_TYPE") == 0)
            $this->load->view('anggota/br-03/peminjaman', $data); //if anggota
    }

    function get_new_bunga($jumlah, $lama, $old_bunga)
    {
        $total_bunga = "";
        $piutang = $jumlah;
        $cicilan_pokok = $jumlah / $lama;

        $total_bunga = 0;

        for ($i = 1; $i <= $lama; $i++)
        {
            $besarbunga = ($old_bunga / 100) * $piutang;
            $total_bunga += $besarbunga;
            $piutang -= $cicilan_pokok;
        }

        return $total_bunga;
    }

    function generate_new_skema()
    {
        $this->auth->is_logged_in('form/login');
        $this->response = new ajaxresponse();

        $info_usaha = $this->input->post('JENIS_PELAYANAN', TRUE);
        $jumlah = $this->input->post('MAX_PIUTANG', TRUE);
        $lama = $this->input->post('MAX_CICIL', TRUE);
        $bunga = $this->input->post('BUNGA', TRUE);

        $jenis_kredit = $this->input->post('TIPE_KREDIT', TRUE);

        if ($info_usaha == 0)
        {
            $info_usaha = $this->input->post('KODE_USAHA', TRUE);
            $jenis_kredit = 2;
        }

        $tgl_pinjaman = $this->input->post("TANGGAL_PINJAM");

        $cicilanpokok = $jumlah / $lama;
        $sisahutang = $jumlah;

        if ($jenis_kredit == 2)
        {//TETAP
            if($info_usaha != 1)//JUAL BELI
                $margin = $this->input->post('MARGIN', TRUE) / $lama;
            else
                $margin = $this->get_new_bunga($jumlah, $lama, $bunga) / $lama;

            $bunga = $margin * 100 / $jumlah;

            for ($i = 1; $i <= $lama; $i++) {
                $sisahutang -= $cicilanpokok;
                $date = date("Y-m-d", strtotime("+$i month", strtotime($tgl_pinjaman)));
                $data[] = (object) array(
                                'date' => $date,
                                'cicilan_ke' => $i,
                                'cicilan_pokok' => $cicilanpokok,
                                'margin' => $margin,
                                'besar_bunga' => $bunga,
                                'angsuran' => $cicilanpokok + $margin,
                                'sisa_hutang' => $sisahutang,
                );
            }
        }
        else if ($jenis_kredit == 3) {//MENURUN
            for ($i = 1; $i <= $lama; $i++) {
                $margin = ($bunga / 100) * $sisahutang;
                $sisahutang -= $cicilanpokok;
                $date = date("Y-m-d", strtotime("+$i month", strtotime($tgl_pinjaman)));
                $data[] = (object) array(
                                'date' => $date,
                                'cicilan_ke' => $i,
                                'cicilan_pokok' => $cicilanpokok,
                                'margin' => $margin,
                                'besar_bunga' => $bunga,
                                'angsuran' => $cicilanpokok + $margin,
                                'sisa_hutang' => $sisahutang,
                );
            }
        }

        $this->response->data = $data;
        echo json_encode($this->response);
    }

    function generate_skema($id_peminjaman) {
        $this->auth->is_logged_in('form/login', 2);
        $datapinjaman = $this->m_pinjaman->get_one($id_peminjaman)->row();

        $lamapinjaman = $datapinjaman->LAMA_PINJAMAN;
        $bunga = $datapinjaman->BUNGA;
        $jumlahpinjaman = $datapinjaman->JUMLAH_PINJAMAN;
        $kredit = $datapinjaman->ID_JENIS_KREDIT;
        $tgl_pinjaman = $datapinjaman->TGL_PINJAMAN;

        $cicilanpokok = $jumlahpinjaman / $lamapinjaman;
        $besarbunga = ($bunga / 100) * $jumlahpinjaman;
        $sisahutang = $jumlahpinjaman;

        if ($kredit == 2) {
            for ($i = 1; $i <= $lamapinjaman; $i++) {
                $sisahutang = $sisahutang - $cicilanpokok;
                $date = date("Y-m-d", strtotime("+$i month", strtotime($tgl_pinjaman)));
                $this->m_angsuran->insert($id_peminjaman, $date, $i, 0, $cicilanpokok, $besarbunga, ($cicilanpokok + $besarbunga), $sisahutang);
            }
        } else if ($kredit == 3) {
            for ($i = 1; $i <= $lamapinjaman; $i++) {
                $besarbunga = ($bunga / 100) * $sisahutang;
                $sisahutang = $sisahutang - $cicilanpokok;
                $date = date("Y-m-d", strtotime("+$i month", strtotime($tgl_pinjaman)));
                $this->m_angsuran->insert($id_peminjaman, $date, $i, 0, $cicilanpokok, $besarbunga, ($cicilanpokok + $besarbunga), $sisahutang);
            }
        } else if ($kredit == 1) {
            $cicilanpokok = ($jumlahpinjaman+$bunga) / $lamapinjaman;
            $besarbunga = 0;
            $sisahutang = ($jumlahpinjaman+$bunga);

            for ($i = 1; $i <= $lamapinjaman; $i++) {
                $sisahutang = $sisahutang - $cicilanpokok;
                $date = date("Y-m-d", strtotime("+$i month", strtotime($tgl_pinjaman)));
                $this->m_angsuran->insert($id_peminjaman, $date, $i, 0, $cicilanpokok, $besarbunga, $cicilanpokok, $sisahutang);
            }
        }
    }

    function add() {
        $this->auth->is_logged_in('form/login', 2);
        $this->response = new ajaxresponse();

        $result = $this->m_pinjaman->insert();
        $newID = $this->db->insert_id();
        if ($newID > 0)
            //$this->generate_skema($newID);
            $this->response->status = "Insert Success";
        else
            $this->response->status = "Error Insert";

        $this->response->data = $newID;
        echo json_encode($this->response);
    }

    function generate_ajax_skema() {
        $this->auth->is_logged_in('form/login', 2);
        $this->response = new ajaxresponse();

        $lamapinjaman = $this->input->post("MAX_CICIL");
        $kredit = $this->input->post("TIPE_KREDIT");
        $margin = $this->input->post("MARGIN");
        $bunga_syariah = $this->input->post("BUNGA_SYARIAH");
        $jumlahpinjaman = $this->input->post("MAX_PIUTANG");
        $kredit = $this->input->post("TIPE_KREDIT");
        $tgl_pinjaman = $this->input->post("TANGGAL_PINJAM");
        $layanan = $this->input->post("JENIS_LAYANAN");

        if ($kredit!=""){
            $bunga = $this->m_jenis_kredit->get_one($kredit)->row()->BESAR_BUNGA;
        }else if ($layanan==0){
            
        }

        $cicilanpokok = $jumlahpinjaman / $lamapinjaman;
        $besarbunga = ($bunga / 100) * $jumlahpinjaman;
        $sisahutang = $jumlahpinjaman;

        if ($kredit == 2) {
            for ($i = 1; $i <= $lamapinjaman; $i++) {
                $sisahutang = $sisahutang - $cicilanpokok;
                $date = date("Y-m-d", strtotime("+$i month", strtotime($tgl_pinjaman)));
                $data[] = (object) array(
                                    'date' => $date,
                                    'cicilan_ke' => $i,
                                    'cicilan_pokok' => $cicilanpokok,
                                    'besar_bunga' => $besarbunga,
                                    'angsuran' => $cicilanpokok + $besarbunga,
                                    'sisa_hutang' => $sisahutang,
                    );
            }
        }
        else if ($kredit == 3) {
            for ($i = 1; $i <= $lamapinjaman; $i++) {
                $besarbunga = ($bunga / 100) * $sisahutang;
                $sisahutang = $sisahutang - $cicilanpokok;
                $date = date("Y-m-d", strtotime("+$i month", strtotime($tgl_pinjaman)));
                $data[] = (object) array(
                                    'date' => $date,
                                    'cicilan_ke' => $i,
                                    'cicilan_pokok' => $cicilanpokok,
                                    'besar_bunga' => $besarbunga,
                                    'angsuran' => $cicilanpokok + $besarbunga,
                                    'sisa_hutang' => $sisahutang,
                    );
            }
        }
        else if ($kredit == 1) {
            $cicilanpokok = ($jumlahpinjaman+$bunga_syariah) / $lamapinjaman;
            $besarbunga = 0;
            $sisahutang = ($jumlahpinjaman+$bunga_syariah);

            for ($i = 1; $i <= $lamapinjaman; $i++) {
                $sisahutang = $sisahutang - $cicilanpokok;
                $date = date("Y-m-d", strtotime("+$i month", strtotime($tgl_pinjaman)));
                $data[] = (object) array(
                                    'date' => $date,
                                    'cicilan_ke' => $i,
                                    'cicilan_pokok' => $cicilanpokok,
                                    'besar_bunga' => 0,
                                    'angsuran' => $cicilanpokok,
                                    'sisa_hutang' => $sisahutang,
                    );
            }
        }

        $this->response->data = $data;
        echo json_encode($this->response);
    }

    function generate_ajax_skema_old() {
        $this->auth->is_logged_in('form/login', 2);
        $this->response = new ajaxresponse();

        $lamapinjaman = $this->input->post("MAX_CICIL");
        $bunga = $this->input->post("BUNGA");
        $bunga_syariah = $this->input->post("BUNGA_SYARIAH");
        $jumlahpinjaman = $this->input->post("MAX_PIUTANG");
        $kredit = $this->input->post("TIPE_KREDIT");
        $tgl_pinjaman = $this->input->post("TANGGAL_PINJAM");

        $cicilanpokok = $jumlahpinjaman / $lamapinjaman;
        $besarbunga = ($bunga / 100) * $jumlahpinjaman;
        $sisahutang = $jumlahpinjaman;

        if ($kredit == 2) {
            for ($i = 1; $i <= $lamapinjaman; $i++) {
                $sisahutang = $sisahutang - $cicilanpokok;
                $date = date("Y-m-d", strtotime("+$i month", strtotime($tgl_pinjaman)));
                $data[] = (object) array(
                                    'date' => $date,
                                    'cicilan_ke' => $i,
                                    'cicilan_pokok' => $cicilanpokok,
                                    'besar_bunga' => $besarbunga,
                                    'angsuran' => $cicilanpokok + $besarbunga,
                                    'sisa_hutang' => $sisahutang,
                    );
            }
        }
        else if ($kredit == 3) {
            for ($i = 1; $i <= $lamapinjaman; $i++) {
                $besarbunga = ($bunga / 100) * $sisahutang;
                $sisahutang = $sisahutang - $cicilanpokok;
                $date = date("Y-m-d", strtotime("+$i month", strtotime($tgl_pinjaman)));
                $data[] = (object) array(
                                    'date' => $date,
                                    'cicilan_ke' => $i,
                                    'cicilan_pokok' => $cicilanpokok,
                                    'besar_bunga' => $besarbunga,
                                    'angsuran' => $cicilanpokok + $besarbunga,
                                    'sisa_hutang' => $sisahutang,
                    );
            }
        }
        else if ($kredit == 1) {
            $cicilanpokok = ($jumlahpinjaman+$bunga_syariah) / $lamapinjaman;
            $besarbunga = 0;
            $sisahutang = ($jumlahpinjaman+$bunga_syariah);

            for ($i = 1; $i <= $lamapinjaman; $i++) {
                $sisahutang = $sisahutang - $cicilanpokok;
                $date = date("Y-m-d", strtotime("+$i month", strtotime($tgl_pinjaman)));
                $data[] = (object) array(
                                    'date' => $date,
                                    'cicilan_ke' => $i,
                                    'cicilan_pokok' => $cicilanpokok,
                                    'besar_bunga' => 0,
                                    'angsuran' => $cicilanpokok,
                                    'sisa_hutang' => $sisahutang,
                    );
            }
        }

        $this->response->data = $data;
        echo json_encode($this->response);
    }

    function skema() {
        $id_peminjaman = $this->input->post("ID_PINJAMAN");
        $datapinjaman = $this->m_pinjaman->get_one($id_peminjaman)->row();

        $lamapinjaman = $datapinjaman->LAMA_PINJAMAN;
        $bunga = $datapinjaman->BUNGA;
        $jumlahpinjaman = $datapinjaman->JUMLAH_PINJAMAN;
        $kredit = $datapinjaman->ID_JENIS_KREDIT;
        $tgl_pinjaman = $datapinjaman->TGL_PINJAMAN;

        $cicilanpokok = $jumlahpinjaman / $lamapinjaman;
        $besarbunga = ($bunga / 100) * $jumlahpinjaman;
        $sisahutang = $jumlahpinjaman;

        for($i = 1; $i <= $lamapinjaman; $i++) {
            $date = date("Y-m-d", strtotime("+$i month", strtotime($tgl_pinjaman)));
            $angsuran = $this->input->post("angsuran_".$id_peminjaman."_".$i);
            $piutang = $this->input->post("piutang_".$id_peminjaman."_".$i);
            $this->m_angsuran->insert($id_peminjaman, $date, $i, 0, $cicilanpokok, $besarbunga, $angsuran, $piutang);
        }
    }

    function data_pinjaman_baru()
    {
        $this->auth->is_logged_in('form/login', 2);
        $this->response = new ajaxresponse();

        $result = $this->m_pinjaman->get("pinjaman.STATUS = 0");
        if($result->num_rows() > 0)
            $this->response->data = $result->result();
        else
        {
            $this->response->status = "EMPTY";
            $this->response->data = 0;
        }
        echo json_encode($this->response);
    }

    function notif_new() {
        $this->auth->is_logged_in('form/login', 2);
        $this->response = new ajaxresponse();
        $this->response->data = $this->m_pinjaman->get("pinjaman.STATUS = 0")->num_rows();
       
        echo json_encode($this->response);
    }

    function list_new() {
        $this->auth->is_logged_in('form/login', 2);
        if ($this->session->userdata("ID_MEMBER_TYPE") == 2) {
            $this->base->head_html('List Peminjaman Baru');
            $this->load->view('pengurus/br-03/list_peminjaman_baru');
        }
    }

    function detail($id = null) {
        $this->auth->is_logged_in('form/login', 2);
        if ($this->session->userdata("ID_MEMBER_TYPE") == 2) {
            $this->base->head_html('Detail Peminjaman');
            $data["Pinjam"] = $this->m_pinjaman->get_one($id)->first_row();
            $this->load->view('pengurus/br-03/app_peminjaman', $data);
        }
    }

    function approve($id)
    {
        $this->auth->is_logged_in('form/login', 2);
        $this->response = new ajaxresponse();

        $result = $this->m_pinjaman->approve($id);
        $this->response->status = "Peminjaman telah diterima.";
        echo json_encode($this->response);
    }

    function reject($id)
    {
        $this->auth->is_logged_in('form/login', 2);
        $this->response = new ajaxresponse();

        $result = $this->m_pinjaman->reject($id);
        $this->response->status = "Peminjaman telah ditolak.";
        echo json_encode($this->response);
    }

    function report($id) {
        $this->load->helper('dompdf');
        $this->load->helper('file');

        $data["Pinjam"] = $this->m_pinjaman->get_one($id)->first_row();
        $data['title'] = "Peminjaman Report";
        //$this->load->view('report/peminjaman', $data);
        $html = $this->load->view('report/peminjaman', $data, true);
        pdf_create($html, 'PJM_' . $data['Pinjam']->NAMA_ANGGOTA . '_' . $data['Pinjam']->ID_PINJAMAN);
    }

    function konsultasi($id) {
        $this->load->helper('dompdf');
        $this->load->helper('file');

        $data["Pinjam"] = $this->m_pinjaman->get_one($id)->first_row();
        $data['title'] = "Konsultasi Form";
        //$this->load->view('report/peminjaman', $data);
        $html = $this->load->view('report/konsultasi_pinjaman', $data, true);
        pdf_create($html, 'KONSUL_' . $data['Pinjam']->NAMA_ANGGOTA . '_' . $data['Pinjam']->ID_PINJAMAN);
    }
}